package gao.xioalei.service;

import gao.xioalei.dao.UserOnlineDao;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("OverService")
public class OverService implements DisposableBean, ExitCodeGenerator {
    @Autowired
    private Environment env;

    @Resource(name = "UserOnlineDao")
    private UserOnlineDao userOnlineDao;

//    @PreDestroy
//    @Transactional
//    public void doClose() {
//        System.out.println("正常执行关闭前的任务begin");
//        System.out.println(userOnlineDao==null);
//        System.out.println(env==null);
//        //将此服务的webSocket数据库记录全部删除
//        userOnlineDao.deleteAllByRoutingKey("79ddf76f2b514c2db0b9e846e6598774");
//        System.out.println("正常执行关闭前的任务end");
//    }

    @Override
    public void destroy() throws Exception {
        System.out.println("正常执行关闭前的任务begin");
        System.out.println(userOnlineDao == null);
        System.out.println(env == null);
        //将此服务的webSocket数据库记录全部删除
        System.out.println(env.getProperty("spring.cloud.stream.rabbit.bindings.webSocketInput.consumer.bindingRoutingKey"));
        userOnlineDao.deleteAllByRoutingKey(env.getProperty("spring.cloud.stream.rabbit.bindings.webSocketInput.consumer.bindingRoutingKey"));
        System.out.println("正常执行关闭前的任务end");
    }

    @Override
    public int getExitCode() {
        return 0;
    }

    @Transactional
    public void test() {
        userOnlineDao.deleteAllByRoutingKey(env.getProperty("spring.cloud.stream.rabbit.bindings.webSocketInput.consumer.bindingRoutingKey"));
    }
}
